package com.minivision.fdiot;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.security.crypto.codec.Utf8;
import org.springframework.util.DigestUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.minivision.fdiot.common.CommonConstants;
import com.minivision.fdiot.domain.DeviceInfo;
import com.minivision.fdiot.domain.FaceSetInfo;
import com.minivision.fdiot.rest.param.device.Sync2DeviceParam;

public class RemoteClient {

  public static void main(String[] args) {
    final String uri = "http://localhost:8080/api/v1/device/sync2Device";
    RestTemplate restTemplate = new RestTemplate();
    Sync2DeviceParam param = new Sync2DeviceParam();
    FaceSetInfo faceSetInfo = new FaceSetInfo();
    faceSetInfo.setFacesetToken("100000");
    List<FaceSetInfo> facesets = new ArrayList<>();
    facesets.add(faceSetInfo);
    param.setFacesets(facesets);
    DeviceInfo deviceInfo = new DeviceInfo();
    deviceInfo.setSn("100000");
    deviceInfo.setModel("box");
    List<DeviceInfo> devices = new ArrayList<>();
    devices.add(deviceInfo);
    param.setDevices(devices);
    long timestamp = System.currentTimeMillis();
    String appKey = "8cc96f98b358bbef29921b14f1656cf0";
    String appSecret = "123456";
    String tokenSecret = appSecret + ":" + timestamp;
    String token = DigestUtils.md5DigestAsHex(Utf8.encode(appKey + "{" + tokenSecret + "}"));
    param.setAppKey(appKey);
    param.setTimestamp("" + timestamp);
    MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    headers.add(CommonConstants.AUTH_HEADER, token);
    HttpEntity<Sync2DeviceParam> request = new HttpEntity<Sync2DeviceParam>(param, headers);
    Object result = restTemplate.postForObject(uri + "?appKey=" + appKey + "&timestamp=" + timestamp, request, Object.class);
    System.out.println(result);
  }

}
